package es.kix2902.testing.android.testing;

import com.admob.android.ads.AdManager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import es.kix2902.testing.android.R;

public class android extends Activity implements TextToSpeech.OnInitListener {
	private TextToSpeech speech;   
	LocationManager locationManager;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		TextView txt = (TextView) findViewById(R.id.txtTexto1);
		txt.requestFocus();

		speech = new TextToSpeech(this, this);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		Button button = (Button) findViewById(R.id.btnToast01);
		button.setOnClickListener(btnToastClick);

		Button button2 = (Button) findViewById(R.id.btnSpeak);
		button2.setOnClickListener(btnSpeakClick);

		Button button3 = (Button) findViewById(R.id.btnUSD);
		button3.setOnClickListener(btnUSDClick);
		Button button4 = (Button) findViewById(R.id.btnGBP);
		button4.setOnClickListener(btnGBPClick);        
		Button button5 = (Button) findViewById(R.id.btnJPY);
		button5.setOnClickListener(btnJPYClick);

		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);;
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

		AdManager.setTestDevices( new String[] {
				AdManager.TEST_EMULATOR,		// Android emulator
				"HT963KF09172",					// My HTC Magic Test Phone
		} );
	}

	@Override
	public void onDestroy() {
		if (speech != null) {
			speech.stop();
			speech.shutdown();
		}
		super.onDestroy();
		locationManager.removeUpdates((LocationListener) this);
	}

	/** Creating the menu **/
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	/** Item of the menu clicked **/
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
		case R.id.mnuSalir:
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/** Code to execute when clicked the button TOAST **/
	private OnClickListener btnToastClick = new OnClickListener() {
		public void onClick(View v) 
		{
			TextView txt = (TextView) findViewById(R.id.txtTexto1);
			Context context = getApplicationContext();
			CharSequence text;
			int duration;

			if (txt.getText().length()==0)
			{
				text = "Debes intoducir un nombre";
				duration = Toast.LENGTH_SHORT;
			} else {
				text = "Hola " + txt.getText() + "!!";
				duration = Toast.LENGTH_LONG;
			}

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
	};

	/** Code executed when initiated TTS **/
	public void onInit(int status) 
	{
		if (status != TextToSpeech.SUCCESS) 
		{
			this.finish();
		}
	}

	/** Code to execute when clicked the button SPEAK **/
	private OnClickListener btnSpeakClick = new OnClickListener() {
		public void onClick(View v) 
		{
			TextView txt = (TextView) findViewById(R.id.txtTexto1);
			if (txt.getText().length() != 0)
			{
				Toast toast = Toast.makeText(getApplicationContext(), "Leyendo", Toast.LENGTH_SHORT);

				toast.show();
				speech.speak("Hola " + txt.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
			}
		}
	};

	/** Obtains the rate exchange EUR->USD **/
	private OnClickListener btnUSDClick = new OnClickListener() {
		public void onClick(View v) 
		{
			Double res = HttpGet.download("USD");

			TextView txt = (TextView) findViewById(R.id.txtConvert);
			if (res==null)
			{
				txt.setText("FAIL");
			}
			txt.setText("Conversion: " + res.toString());
		}
	};

	/** Obtains the rate exchange EUR->GBP **/
	private OnClickListener btnGBPClick = new OnClickListener() {
		public void onClick(View v) 
		{
			TextView txt = (TextView) findViewById(R.id.txtConvert);
			txt.setText("Conversion: " + HttpGet.download("GBP").toString());
		}
	};

	/** Obtains the rate exchange EUR->JPY **/
	private OnClickListener btnJPYClick = new OnClickListener() {
		public void onClick(View v) 
		{
			TextView txt = (TextView) findViewById(R.id.txtConvert);
			txt.setText("Conversion: " + HttpGet.download("JPY").toString());
		}
	};

	LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			((TextView) findViewById(R.id.txtPos)).setText(Double.toString(location.getLatitude()) + ", " + Double.toString(location.getLongitude()));
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {}

		public void onProviderEnabled(String provider) {}

		public void onProviderDisabled(String provider) {}
	};
	
}